<?php 
	error_reporting(0);
	session_start();
 ?>
<!DOCTYPE HTML>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>商品后台管理 - 一书网</title>
	<link rel="stylesheet" type="text/css" href="style/admin_index.css">
	<link rel="stylesheet" type="text/css" href="style/admin_adding.css">
</head>
<body>
	<?php 
		if($_GET["action"] == 'logout' || $_SESSION['admin_name'] === null) {
			session_unset();
	 		echo '<p style="text-align:center;margin-top:50px;">
	 				系统检测到您当前处于未登录状态，点击此处 <a href="admin_login.php">登录</a>。
	 			  </p>';
	 		exit;
 		}
	 ?>
	<div class="top-nav">
		<div class="option-info">
			<div class="option">
				<span>请选择你要进行的操作：</span>
				<select id="select" onChange="Tab();">  
					<option value="1" selected="">添加书籍</option> 
					<option value="2" selected="">修改书籍信息</option>
					<option value="3" selected="">删除书籍</option>
					<option value="4" selected="">查看书籍信息</option>
					<option value="5" selected="">查看所有用户订单</option>
					<option value="0" selected=""><span class="curr-title">
						<?php 
							if($_GET['f'] == "add") {
								echo "添加书籍";
							} else if($_GET['f'] == "revise") {
								echo "修改书籍信息";
							} else if($_GET['f'] == "delete"){
								echo "删除书籍";
							} else if($_GET['f'] == "view"){
								echo "查看书籍信息";
							} else if($_GET['f'] == "view_order") {
								echo "查看所有用户订单";
							} else {
								echo "请选择操作";
							}
						?></span>
					</option>
				</select>   
			</div>
			<div class="admin-info">
				<span>当前用户：<?php echo $_SESSION['admin_name']; ?>　　</span>
				<span>登录时间：<?php echo date("Y-m-d",time()); ?>　　</span>
				<span><a href="admin_index.php?action=logout">注销</a></span>
			</div>
		</div>	
	</div>
	<?php 
		switch ($_GET['f']) {
			// 添加书籍商品
			case "add":
				echo "<div id='content-add'>
						<h2>添加书籍</h2><hr>
						<form action='action.php?action=add' method='post' enctype='multipart/form-data'>
							<div class='input'>
								　书　名：<input type='text' name='bookName' value=''/>
							</div>
							<div class='input'>
								　作　者：<input type='text' name='author' value='' />
							</div>
							<div class='input'>
								　版　次：<input type='text' name='version' value='' />
							</div>
							<div class='input'>
								　出版社：<input type='text' name='press' value='' />
							</div>
							<div class='input'>
								出版日期：<input type='text' name='pressDate' value='' />
							</div>
							<div class='input'>
								　库　存：<input type='text' name='totalNum' value='' />
							</div>
							<div class='input'>
								　单　价：<input type='text' name='price' value='' />
							</div>
							<div class='input'>
								　图　片：<input type='file' name='pic' value='' />
							</div>
							<div class='input'>
								<input class='submit' type='submit' value='添加'>
								<input class='reset' type='reset' value='重置'>
							</div>
						</form>
					</div>";
				break;
				
			// 修改指定书籍信息
			case "revise":
				if($_GET['f'] == "revise" && $_GET['isbn'] != null) {
					$isbn = $_GET['isbn'];
					include 'link.php';
					$sql = "select * from books where ISBN = '$isbn'";
					$result = mysqli_query($link, $sql);
					$rows = mysqli_fetch_array($result);

					echo "<h2>修改书籍信息</h2><hr>
							<form action='action.php?action=revise&isbn=$isbn' method='post' enctype='multipart/form-data'>
								<div class='input'>
									　书　名：<input type='text' name='bookName' value='{$rows['book_name']}'/>
								</div>
								<div class='input'>
									　作　者：<input type='text' name='author' value='{$rows['author']}' />
								</div>
								<div class='input'>
									　版　次：<input type='text' name='version' value='{$rows['version']}' />
								</div>
								<div class='input'>
									　出版社：<input type='text' name='press' value='{$rows['press_title']}' />
								</div>
								<div class='input'>
									出版日期：<input type='text' name='pressDate' value='{$rows['press_date']}' />
								</div>
								<div class='input'>
									　库　存：<input type='text' name='totalNum' value='{$rows['total_num']}' />
								</div>
								<div class='input'>
									　单　价：<input type='text' name='price' value='{$rows['price']}' />
								</div>
								<div class='input'>
									<input class='submit' type='submit' value='修改'>
									<input class='reset' type='reset' value='重置'>
								</div>
							</form>";
				} else {
					echo "<div id='content-revise'>
						<form action='action.php?action=select_isbn' method='post'>
							<div class='input-isbn'>
								请输入你要修改书籍的ISBN：<input type='text' name='isbn' value='' />
							</div>
							<div class='input'>
								<input class='submit' type='submit' value='修改' />
								<input class='reset' type='reset' value='重置' />
							</div>
						</form>
					</div>";
				}
				break;
				
			// 删除书籍
			case "delete";
				echo "<div id='content-delete'>
						<h2>删除书籍</h2>
						<hr />
						<form action='action.php?action=delete' method='post'>
							<div class='input-isbn'>
								请输入你要删除书籍的ISBN：<input type='text' name='isbn' value=''/>
							</div>
							<div class='input'>
								<input class='submit' type='submit' value='删除'>
								<input class='reset' type='reset' value='重置'>
							</div>
						</form>
					</div>";
				break;
				
			// 查看书籍
			case "view":
				// 1、导入连接文件
				include 'link.php';
				
				// 2、生成表头结构及信息
				echo "<table>
						  <caption>
						  	<h1>书籍信息浏览</h1>
						  	<hr />
						  </caption>
						  <thead>
						    <tr>
						      <th>ISBN</th>
						      <th>书名</th>
						      <th>作者</th>
						      <th>版次</th>
						      <th>出版社</th>
						      <th>出版日期</th>
						      <th>库存</th>
						      <th>单价</th>
						    </tr>
						  </thead>
						<tbody id='tab'> ";

				// 3、执行sql查询
				$sql = "select * from books";
				$result = mysqli_query($link, $sql);

				// 定位到第一条记录
 				mysqli_data_seek($result, 0);
				
				// 4、循环取出记录,生成展示商品信息的表格
				if($result){
					while ($rows = mysqli_fetch_array($result)){
						echo "<tr>
							<td>{$rows['ISBN']}</td>
							<td>{$rows['book_name']}</td>
							<td>{$rows['author']}</td>
							<td>{$rows['version']}</td>
							<td>{$rows['press_title']}</td>
							<td>{$rows['press_date']}</td>
							<td>{$rows['total_num']}</td>
							<td>{$rows['price']}</td>
							</tr>";
					}
				}
				echo "</tbody><table/>";
				break;

			// 查看书籍
			case "view_order":
				// 1、导入连接文件
				include 'link.php';
				
				// 2、生成表头结构及信息
				echo "<table>
						  <caption>
						  	<h1>用户订单信息</h1>
						  	<hr />
						  </caption>
						  <thead>
						    <tr>
						      <th>订单号</th>
						      <th>书名</th>
						      <th>订购数</th>
						      <th>实付金额</th>
						      <th>收货人</th>
						      <th>收货人电话</th>
						      <th>快递公司</th>
						      <th>订单状态</th>
						      <th>操作</th>
						    </tr>
						  </thead>
						<tbody id='tab'>";

				// 3、执行sql查询
				$sql = "select * from ship";
				$result = mysqli_query($link, $sql);

				// 定位到第一条记录
 				mysqli_data_seek($result, 0);
				
				// 4、循环取出记录,生成展示商品信息的表格
				if($result){
					while ($rows = mysqli_fetch_array($result)){
						echo "<tr>
							<td>{$rows['ship_no']}</td>
							<td>{$rows['book_name']}</td>
							<td>{$rows['order_quantity']}</td>
							<td>{$rows['amount']}</td>
							<td>{$rows['receiver']}</td>
							<td>{$rows['ship_tel']}</td>
							<td>{$rows['ship_company']}</td>
							<td>{$rows['ship_state']}</td>";
							if($rows['ship_state'] == '已发货') {
								echo "<td>
										/
									  </td>
									</tr>";
							} else {
								echo "<td>
										<a href='handle_ship.php?id={$rows['ship_no']}'>接单并配送</a>
									  </td>
									</tr>";
							}
					}
				}
				echo "</tbody><table/>";
				break;

			default:
				echo "<p style='text-align:center;margin-top:50px;font-size:16px;'>欢迎{$_SESSION['admin_name']}！请在导航栏选择你所要进行的操作。</p>";
				break;
		}
		// 关闭数据库连接
		mysqli_close($link);
	 ?>
	<script type="text/javascript" src="js/admin_index.js"></script>
</body>
</html>